# coding=utf-8
from django.db import transaction
from django.http import JsonResponse
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import permissions
from rest_framework import viewsets, mixins
from report.serializers import *
from report.models import *

import pdfkit

from utils import logger
from utils.pagination import SmallResultsSetPagination


class ReportViewSet(viewsets.GenericViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = ReportSerializers

    def get_pdf(self, request):
        try:
            url='http://127.0.0.1/#/'#一篇博客的url
            confg = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
            #这里指定一下wkhtmltopdf的路径，这就是我为啥在前面让记住这个路径
            # pdfkit.from_url(url, 'jmeter_下载文件.pdf',configuration=confg)
            # from_url这个函数是从url里面获取内容
            pdfkit.from_file('test.html', 'out.pdf')
            # 这有3个参数，第一个是url，第二个是文件名，第三个就是khtmltopdf的路径
            results = {}
            return JsonResponse({'code': 200, "message": "获取成功", "data": results}, status=200)
        except Exception as e:
            return JsonResponse({'code': 400, "message": "获取参数失败" + str(e)}, status=400)
